//自定义网页编辑插件
;(function($){
	//存储参数
	var deParams = {
			
	};
	
	var defaultParams = {
			//定义初始化时宽高，以屏幕宽度为基准的百分比
			defaultWidth : 10,
			defaultHeight : 10,
			defaultLeft : 0,
			defaultTop : 0,
			defaultZindex : 1,
			lboxWidth : 0.01//设定拖动小格子一格为宽度的百分之1
	};
	
    $.fn.deBox=function(params,data){
    	params.box = $(this);
    	if("getParam" == params){
    		var uuid = $(this).data("uuid");
    		return deParams[uuid][data];
    	}else if("getParams" == params){
    		var uuid = $(this).data("uuid");
    		return deParams[uuid];
    	}else if("getInfo" == params){
    		return getInfo($(this),data);
    	}else if("setProps" == params){
    		deParams[data.uuid].opts = data.props;
    		var box = deParams[data.uuid].stage.webeditor("getBox",data.uuid);
    		var stageWidth = deParams[data.uuid].stage.width();
    		setBoxPropsRender(stageWidth,deParams[data.uuid].parentWidth,box,data.props,true);
    	}else if("getUuid" == params){
    		return $(this).data("uuid");
    	}else{
    		//初始化盒子
    		return buildBox(params,data);
    	}
    }
    
    //新建盒子
    function buildBox(params,info){
    	var uuid = CreateUuid();
		params.box.data("uuid",uuid);
		params.box.data("myid",info.id);
		deParams[uuid] = {
			stage : params.stage,
			parentWidth: params.parentWidth,
			opts:info.opts,
			optsContainer: params.optsContainer,
			showContainer: params.showContainer,
			optsCurCon: params.optsCurCon
		};
    	params.box.append($("<div class='elementsBox'></div>"));
		params.box.append($("<div class='removeit'><img class='crossimg' src='"+ctx+"/resources/js/easyui/themes/iconsextension/bullet_cross.png'/></div>"));
    	//params.box.append($("<div class='coor'></div>"));
    	params.box.bind("click",function(){
    		var curData = $(this).data("iscurbox");
    		var doRender = true;
    		if("1" == curData){
    			doRender = false;
    		}
    		if(doRender){
    			$(".clickedBiggerboxs").data("iscurbox","2");
    			$(".clickedBiggerboxs").css("margin","0px");
    			$(".clickedBiggerboxs").css("padding","0px");
    			$(this).data("iscurbox","1");
    			$(".clickedBiggerboxs").removeClass("clickedBiggerboxs");
    			$(this).addClass("clickedBiggerboxs");
    			$(this).css("margin","-1px");
    			$(this).css("padding","-1px");
    			params.optsCurCon.val(uuid);
    			params.optsContainer.data("editinguuid",uuid);
    			params.showContainer.data("editinguuid",uuid);
    			var pcontents = $.extend(info.opts,deParams[uuid].opts);
    			params.optsContainer.html("");
    			params.showContainer.html("");
    			for(var i = 0; i < pcontents.length; i++){
    				var appendType = "prop";
    				var options = pcontents[i];
    				var id = options.id;
    				var pcid = options.pcid;
    				if(null == id || undefined == id || "undefined" == id){
    					id = "";
    				}
    				if(null == pcid || undefined == pcid || "undefined" == pcid){
    					pcid = "";
    				}
    				var type = "text";
    				var dataRenderType = "text";
    				if("color" == options.text || "background-color" == options.text || "border-color" ==  options.text){
    					type = "color";
    					dataRenderType = "color";
    				}
    				if("imgurl-s" ==  options.text){
    					dataRenderType = "imgurl-s";
    				}else if("imgurl-o" ==  options.text){
    					dataRenderType = "imgurl-o";
    				}else if("border-style" ==  options.text){
    					dataRenderType = "border-style";
    				}else if("requirement" ==  options.text){
    					dataRenderType = "requirement";
    				}else if("z-index" ==  options.text){
    					dataRenderType = "number";
    				}else if("opacity" ==  options.text){
    					dataRenderType = "numbermax1";
    				}else if("text-align" ==  options.text){
    					dataRenderType = "textalign";
    				}else if("font-weight" ==  options.text){
    					dataRenderType = "fontweight";
    				}else if("position" ==  options.text){
    					dataRenderType = "position";
    				}else if("divhreftype" ==  options.text){
    					dataRenderType = "divhreftype";
    				}else if("iframedivhreftype" ==  options.text){
    					dataRenderType = "iframedivhreftype";
    				}else if("showtype" ==  options.text){
    					dataRenderType = "showtype";
    					appendType = "show";
    				}else if("showtime" ==  options.text){
    					dataRenderType = "showtime";
    					appendType = "show";
    				}else if("animatetype" ==  options.text){
    					dataRenderType = "animatetype";
    					appendType = "show";
    				}else if("animatetime" ==  options.text){
    					dataRenderType = "animatetime";
    					appendType = "show";
    				}else if("clickshow" ==  options.text){
    					dataRenderType = "clickshow";
    					appendType = "show";
    				}else if("clickshowcolor" ==  options.text){
    					appendType = "show";
    					type = "color";
    					dataRenderType = "color";
    				}else if("clickcolor" ==  options.text){
    					appendType = "show";
    					type = "color";
    					dataRenderType = "color";
    				}else if("clickfontweight" ==  options.text){
    					appendType = "show";
    					dataRenderType = "clickfontweight";
    				}else if("sfmrdj" ==  options.text){
    					appendType = "show";
    					dataRenderType = "sfmrdj";
    				}
    				var dcontent = options.content;
    				var inputbox = "<input style='width:150px;' type='"+type+"' data-cuuid='"+uuid+"' data-myid='"+id+"' data-type='"+options.type+"' data-rtype = '"+dataRenderType+"' data-pcid='"+pcid+"' data-title='"+options.title+"' data-text='"+options.text+"' data-pindex='"+options.pindex+"' data-content='"+dcontent+"' value='"+dcontent+"'/>";
    				
    				var propDiv = $("<div class='optEditor'>" +
    									"<div class='optEditor1'>"+options.title+"</div>" +
    									"<div class='optEditor2'>" +
    											inputbox +
    									"</div>" +
    								"</div>");
					if("prop" == appendType){
						params.optsContainer.append(propDiv);
					}else{
						params.showContainer.append(propDiv);
					}
    			}
    			window.parent.renderPropbox();
    		}
		});
    	var decon = $(info.detailCode);
		decon.addClass("decons");
		params.box.find(".elementsBox").append(decon);
		params.box.find(".elementsBox").find(".iframezzc").remove();
		
		for(var i = 0; i < info.opts.length; i++){
			if(info.opts[i].type == "100"){
				if(!info.width && "width" == info.opts[i].text){
					info.width = info.opts[i].content;
				}
				if(!info.height && "height" == info.opts[i].text){
					info.height = info.opts[i].content;
				}
				if(!info.zindex && "z-index" == info.opts[i].text){
					info.zindex = info.opts[i].content;
				}
			}
		}
		
		//计算宽高等
		var stageWidth = params.stage.width();
		
		//绑定拖动
		//设定拖动格子
		if("phone" == params.mode){
			defaultParams.lboxWidth = 0.02;
		}
		var lboxwidth = defaultParams.lboxWidth * stageWidth;
		params.box.Tdrag({
			scope:".boxList",
			grid:[lboxwidth,lboxwidth]
		});
		setBoxPropsRender(stageWidth,params.parentWidth,params.box,info.opts,true);
		//绑定放大缩小
		seriBox(params,uuid);
		commonParser(params,info,defaultParams,stageWidth,true);
		return params.box;
    }
    //渲染盒子
    function seriBox(params,uuid) {
    	var box = params.box;
		var removeit = box.find(".removeit");
		
		removeit.unbind().bind("click",function(){
			box.remove();
			params.stage.webeditor("deleteOne",uuid);
			params.optsContainer.html("");
			params.showContainer.html("");
			params.optsCurCon.val("");
			params.optsContainer.data("editinguuid","");
			params.showContainer.data("editinguuid","");
		});
    }
    
    //获取详情
    function getInfo(obj,data){
    	var uuid = obj.data("uuid");
    	var dparams = deParams[uuid];
    	var stageWidth = data.stageWidth;
    	var pageId = data.pageId;
    	var id = obj.data("myid");
		if(null == id || "" == id || undefined == id || "undefined" == id){
			id = null;
		}
		obj.find(".elementsBox").find(".iframezzc").hide();
		var detailCode = obj.find(".elementsBox").html();
		obj.find(".elementsBox").find(".iframezzc").show();
		var mleft = obj.position().left * 100 / stageWidth;
		var top;
		var position = obj.css("position");
		if("fixed" == position){
			//固定位以该控件的距离顶部和去除控件高度后屏幕剩余高度的百分比来实现
			var wheight = $(window).height();
			
			var xtop = obj.position().top /(wheight - obj.height());
			top = xtop;
		}else{
			top = obj.position().top * 100 / stageWidth;
		}
		var width = obj.width() * 100 / stageWidth;
		var height = obj.height() * 100 / stageWidth;
		
		var zindex = obj.css('z-index');
		for(var xx = 0; xx < dparams.opts.length; xx++){
			var optsx = dparams.opts[xx];
			if("z-index" == optsx.text){
				zindex = optsx.content;
			}
		}
		if("auto" == zindex){
			zindex = 1;
		}
		var styleCode = "";
		var pcontents = new Array();
		var decon = $(obj.find(".elementsBox").find(".decons")[0]);
		if(decon){
			var opts = obj.deBox("getParam","opts");
			if(null != opts){
				pcontents = opts;
			}
		}
		var infoObj = {
				id : id,
				pageId : pageId,
				detailCode:detailCode,
				mleft : mleft,
				top : top,
				width : width,
				height : height,
				zindex : zindex,
				styleCode : styleCode,
				pcontents : pcontents
			};
		return infoObj;
    }
    
    function CreateUuid(){
    	var mydate = new Date();
    	var rndNum = RndNum(6);
    	return "cons"+mydate.getDay()+ mydate.getHours()+ mydate.getMinutes()+mydate.getSeconds()+mydate.getMilliseconds()+rndNum;
    }
    
    function RndNum(n){
        var rnd="";
        for(var i=0;i<n;i++)
            rnd+=Math.floor(Math.random()*10);
        return rnd;
    }
})(jQuery); 